﻿package rd.hardcore.ui 
{
	import aze.motion.eaze;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Graphics;
	import flash.display.Shader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.SoundMixer;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import rd.hardcore.events.SoundSystemEvent;
	/**
	 * ...
	 * @author Thibault Perret
	 */
	public class EqVisualization2 extends Sprite
	{
		private const PLOT_HEIGHT : int = 95;
		private const CHANNEL_LENGTH:int = 80;
		
		private var ba:ByteArray;
		private var sp:Sprite;
		private var g:Graphics;
		private var bmpD:BitmapData;
		private var toBlur:Sprite;
		
		private var soundPlay : Boolean = false;
		private var endTimer:Timer;
		
		public function EqVisualization2() 
		{
			if (stage)	init(null);
			else		addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			ba = new ByteArray();
			
			toBlur = new Sprite();
			toBlur.x = 640;
			toBlur.y = -40;
			addChild(toBlur);
			
			sp = new Sprite();
			addChild(sp);
			sp.alpha = 0;
			g = sp.graphics;
			
			bmpD = new BitmapData(200, 200, true, 0x00000000);
		}
		
		public function pixelize():void
		{
			addEventListener(Event.ENTER_FRAME, loop);
			soundPlay = true;
			eaze(toBlur).to(1, { alpha:2 } );
		}
		
		public function stopPixelize(e:SoundSystemEvent):void
		{
			soundPlay = false;
			endTimer = new Timer(1500);
			endTimer.addEventListener(TimerEvent.TIMER, handleendTimer);
			endTimer.start();
		}
		
		private function handleendTimer(e:TimerEvent):void 
		{
			endTimer.removeEventListener(TimerEvent.TIMER, handleendTimer);
			endTimer.stop();
			removeEventListener(Event.ENTER_FRAME, loop);
			eaze(toBlur).to(1, { alpha:0 } );
		}
		
		private function loop(e:Event):void 
		{
			
			if (soundPlay){
				SoundMixer.computeSpectrum(ba, false);
				g.clear();
				
				var n:Number = 0;
				
				g.beginFill(0xbb0000, .5);
				for (var i:int = 0; i < CHANNEL_LENGTH; i++) {
					n = (ba.readFloat() * PLOT_HEIGHT);
					g.drawCircle(Math.cos(i * 2), Math.sin(PLOT_HEIGHT - n), n*.5);
				}
				
				g.endFill();
				
				g.beginFill(0x990000, .5);
				for (i = CHANNEL_LENGTH; i > 0; i--) {
					n = (ba.readFloat() * PLOT_HEIGHT);
					g.drawCircle(15+(20+n)*Math.cos((2*Math.PI * i)/CHANNEL_LENGTH), 20+(20+n)*Math.sin((2*Math.PI * i)/CHANNEL_LENGTH), n*.3);
				}
				
				g.endFill();
			}
			
			bmpD = new BitmapData(200, 200, true, 0x00000000);
			var mtr:Matrix = new Matrix();
			mtr.translate( 70, 50 );
			bmpD.draw(sp,mtr);
			
			var bmp:Bitmap = new Bitmap(bmpD);
			var pt:Point = new Point(0, 0);
			var rect:Rectangle = new Rectangle(0, 0, bmpD.width, bmpD.height);
			var filter:BlurFilter = new BlurFilter(4, 4, BitmapFilterQuality.HIGH);
			bmpD.applyFilter(bmpD, rect, pt, filter);
			toBlur.addChild(bmp);
			toBlur.blendMode = BlendMode.MULTIPLY;
			
			
			
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
		}
		
	}

}